package com.centaline.androidsalesblog.activities.map;

import android.content.Intent;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.widget.PopupWindow;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.RentDetailActivity;
import com.centaline.androidsalesblog.activities.sale.SaleDetailActivity;
import com.centaline.androidsalesblog.adapter.TabFragmentAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.fragments.map.NewEstMainMapFragment;
import com.centaline.androidsalesblog.fragments.map.RentMainMapFragment;
import com.centaline.androidsalesblog.fragments.map.SaleMainMapFragment;
import com.centaline.androidsalesblog.iml.PostMapCallback;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.widgets.IndexViewPager;
import com.centaline.androidsalesblog.widgets.PostMapPop;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 地图找房
 * Created by guilin on 16/4/13.
 */
public class MainMapActivity extends BaseActivity {

    private CoordinatorLayout coordinatorLayout;
    private TabLayout tl_map;
    private IndexViewPager viewPager;

    private SaleMainMapFragment saleMainMapFragment;
    private RentMainMapFragment rentMainMapFragment;

    private PostMapPop postMapPop;
    private ArrayList<PostDo> postDoArrayList = new ArrayList<>();

    private String postType;
    private int iRefreshType;

    private PostMapCallback<EsfEstateDo> postMapCallback = new PostMapCallback<EsfEstateDo>() {
        @Override
        public void callBottomSheet(String type, EsfEstateDo esfEstateDo) {
            postType = type;
            postMapPop.show(postType, esfEstateDo);
        }

        @Override
        public void callBack(String type, ArrayList<PostDo> temp) {
            postType = type;
            if (iRefreshType == 1) {
                postDoArrayList.clear();
            }
            if (temp == null) {
                postMapPop.stopRefresh(false);
            } else {
                postDoArrayList.addAll(temp);
                postMapPop.stopRefresh(temp.size() == 10);
            }
        }
    };

    private IRecycleViewListener iRecycleViewListener = new IRecycleViewListener() {
        @Override
        public void downRefresh() {
            iRefreshType = 1;
            if ("s".equalsIgnoreCase(postType)) {
                saleMainMapFragment.downRequest();
            } else {
                rentMainMapFragment.downRequest();
            }
        }

        @Override
        public void upRefresh() {
            iRefreshType = 2;
            if ("s".equalsIgnoreCase(postType)) {
                saleMainMapFragment.upRequest(postDoArrayList.size());
            } else {
                rentMainMapFragment.upRequest(postDoArrayList.size());
            }
        }

        @Override
        public void onItemClick(int position) {
            if ("s".equalsIgnoreCase(postDoArrayList.get(position).getPostType())) {
                startActivity(new Intent(MainMapActivity.this, SaleDetailActivity.class)
                        .putExtra(ExtraConstant.PostId, postDoArrayList.get(position).getPostId()));
            } else {
                startActivity(new Intent(MainMapActivity.this, RentDetailActivity.class)
                        .putExtra(ExtraConstant.PostId, postDoArrayList.get(position).getPostId()));
            }
        }
    };

    private PopupWindow.OnDismissListener onDismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            if ("s".equalsIgnoreCase(postType)) {
                saleMainMapFragment.updateMapStatusByBottomPop(true);
            } else {
                rentMainMapFragment.updateMapStatusByBottomPop(true);
            }
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main_map;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        tl_map = (TabLayout) findViewById(R.id.tl_map);
        viewPager = (IndexViewPager) findViewById(R.id.viewPager);
    }

    @Override
    protected void initViews() {
        postMapPop = new PostMapPop(this, coordinatorLayout, postDoArrayList,
                GlideLoad.init(this), onDismissListener, iRecycleViewListener);
        bizUnit();
    }

    /**
     * 初始化
     */
    private void initFragments(BizUnitDo bizUnitDo) {
        if (bizUnitDo != null) {
            boolean saleEnable = bizUnitDo.isEnableErshoufang();//二手房是否可用
            boolean newPropEnable = bizUnitDo.isEnableNewprop();//新房是否可用
            ArrayList<String> titleList = new ArrayList<>();
            ArrayList<Fragment> fragments = new ArrayList<>();
            if (saleEnable) {
                titleList.add("二手房");
                saleMainMapFragment = new SaleMainMapFragment();
                saleMainMapFragment.setPostMapCallback(postMapCallback);
                fragments.add(saleMainMapFragment);
            }
            if (newPropEnable) {
                titleList.add("新房");
                NewEstMainMapFragment newEstMainMapFragment = new NewEstMainMapFragment();
                fragments.add(newEstMainMapFragment);
            }
            if (saleEnable) {
                titleList.add("租房");
                rentMainMapFragment = new RentMainMapFragment();
                rentMainMapFragment.setPostMapCallback(postMapCallback);
                fragments.add(rentMainMapFragment);
            }
            viewPager.setAdapter(new TabFragmentAdapter(getSupportFragmentManager(), fragments, titleList));
            viewPager.setOffscreenPageLimit(2);
            tl_map.setupWithViewPager(viewPager);
        }
    }

    /**
     * 获取配置信息
     */
    private void bizUnit() {
        RxDataBase.getBizUnit()
                .compose(this.<BizUnitDo>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        initFragments(bizUnitDo);
                    }
                });
    }
}
